package cn.hh.dictionary.controller;


import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.dictionary.domain.Detail;
import cn.hh.dictionary.domain.Type;
import cn.hh.dictionary.query.TypeQuery;
import cn.hh.dictionary.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private ITypeService typeService;
    /*编辑*/
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Type type){
        /*通过id判断是新增还是删除*/
        try {
            if (type.getId()==null){
                typeService.add(type);
            }else {
                typeService.update(type);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            typeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            typeService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*查询所有*/
    @GetMapping
    public List<Type> loadAll(){
        return typeService.loadAll();
    }
    /*分页*/
    @PostMapping
    public PageList<Type> queryPage(@RequestBody TypeQuery query){
        return typeService.queryPage(query);
    }
    @PostMapping("/detail")
    public PageList<Detail> findById(@RequestBody TypeQuery query){
        return typeService.findDetailById(query);
    }
}
